
from re import A
from unicodedata import name
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from pip import main
from sqlalchemy import ForeignKey, false, true

app = Flask(__name__)

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + "first.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False                 
app.config['SECRET_KEY']='qinghuan'                                  #配置key

db = SQLAlchemy(app)
# 学生表
class student(db.Model):                            #建表命名为student
    _tablename_ =   'student'                       #表名   
    id = db.Column(db.Integer, primary_key=True)    # 主键，ID为Interger型
    name = db.Column(db.String(64),nullable=false)  #姓名，字符串类型,nullalbe能否为空
    grades = db.relationship('chengji',backref='student')   #‘类名’，backef填写当前表名
    courses = db.relationship('course',secondary='student_to_course',backref='student')    #seconddary放中间表的表名
# 成绩表
class chengji(db.Model):
    _tablename_ = 'chengji'
    id = db.Column(db.Integer,primary_key=true)
    chengji = db.Column(db.String(3),nullable=False)
    student_id = db.Column(db.Integer,db.ForeignKey('student.id'))      #外键指向student表内的id
# 课程表
class course(db.Model):
    _tablename_ =   'course'
    id = db.Column(db.Integer,primary_key=true)
    name = db.Column(db.String(64),nullable=false)
# 中间表
class StudenToCourse(db.Model):
    __tablename__ = "student_to_course"
    id = db.Column(db.Integer, primary_key=True)  # 主键
    student_id = db.Column(db.Integer, db.ForeignKey("student.id"))  # 所属学生
    course_id = db.Column(db.Integer, db.ForeignKey("course.id"))  # 所属课程


#创建数据库
if  __name__ == '__main__':             
    db.create_all()
    # db.drop_all()       #清空数据库